package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.ToDoItem;
import com.ldatum.crm.service.TCustomerService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.service.ToDoItemService;
import com.ldatum.crm.serviceImpl.TCustomerServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;
import com.ldatum.crm.serviceImpl.ToDoItemServiceImpl;

@ParentPackage("admin")

public class DbsxAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ToDoItem toDoItem = new ToDoItem();
	
	List<ToDoItem> toDoItemList = new ArrayList<ToDoItem>();
	List<TCustomer> customerList = new ArrayList<TCustomer>();
	List<TEmployee> employeeList = new ArrayList<TEmployee>();
	
	ToDoItemService toDoItemService = new ToDoItemServiceImpl();
	TCustomerService customerService = new TCustomerServiceImpl();
	TEmployeeService employeeService = new TEmployeeServiceImpl();
	
	private int id;
	private String theme;
	private String remindTime;
	private String follower;
	private String status;
	private String finishTime;
	private String lxr;
	private String lxdh;
	private String detail;
	private String createTime;
	
	private String levelOne;
	private String levelTwo;
	
	public void viewById(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			toDoItem = toDoItemService.findById(id);
			json.accumulate("toDoItem", toDoItem);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String findById(){
		toDoItem = toDoItemService.findById(id);
		try {
			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			customerList = customerService.myCustomer(TCustomer.class,currentEmployee); //我的客户列表
			employeeList = employeeService.list(TEmployee.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "dailyManagement";
		levelTwo = "toDoItem";
		return "editToDoItem";
	}
	
	public void save(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			toDoItemService.save(toDoItem);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	

	public void update(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			toDoItem = getScheduleFromPage();
			toDoItemService.update(toDoItem);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String list() throws Exception{
		String creater = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
		toDoItemList = toDoItemService.list(ToDoItem.class,creater);
		levelOne = "dailyManagement";
		levelTwo = "toDoItem";
		return "listToDoItem";
	}
//	@Test
	public String add() throws Exception{
		try {
			follower = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
//			customerList = customerService.myCustomer(TCustomer.class,currentEmployee); //我的客户列表
//			employeeList = employeeService.list(TEmployee.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "dailyManagement";
		levelTwo = "toDoItem";
		return "addToDoItem";
	}

	public String edit() throws Exception{
		try {
			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			toDoItem = toDoItemService.findById(id);
			customerList = customerService.myCustomer(TCustomer.class,currentEmployee); //我的客户列表
			employeeList = employeeService.list(TEmployee.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "toDoItem";
		return "editSchedule";
	}
	
	public void delete(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			toDoItemService.delete(id);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public ToDoItem getScheduleFromPage() {
		
		return toDoItem;
		
	}

	public ToDoItem getToDoItem() {
		return toDoItem;
	}

	public void setToDoItem(ToDoItem toDoItem) {
		this.toDoItem = toDoItem;
	}

	public List<ToDoItem> getToDoItemList() {
		return toDoItemList;
	}

	public void setToDoItemList(List<ToDoItem> toDoItemList) {
		this.toDoItemList = toDoItemList;
	}

	public List<TCustomer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getRemindTime() {
		return remindTime;
	}

	public void setRemindTime(String remindTime) {
		this.remindTime = remindTime;
	}

	public String getFollower() {
		return follower;
	}

	public void setFollower(String follower) {
		this.follower = follower;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(String finishTime) {
		this.finishTime = finishTime;
	}

	public String getLxr() {
		return lxr;
	}

	public void setLxr(String lxr) {
		this.lxr = lxr;
	}

	public String getLxdh() {
		return lxdh;
	}

	public void setLxdh(String lxdh) {
		this.lxdh = lxdh;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}
	
}
